//
//  RCMerchantDetailTrafficCell.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-9-10.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCMerchantDetailTrafficCell.h"
#import "NSString+NSString_Ex_.h"
#import "UIColor-Expanded.h"
#import "CommonDefines.h"
#import "TTTAttributedLabel.h"
#import "NSAttributedString+LVAddition.h"
#import "RCMerchantDetailTrafficDataModel.h"
#import "NSString+NSString_Ex_.h"

#define kEmailImageFile              @"image_merchant_traffic_email.png"
#define kTextBGImageFile             @"image_about_textBG.png"

@interface RCMerchantDetailTrafficCell()

@property(nonatomic,retain)UIImageView *backgroundImageView;
@property(nonatomic,retain)UILabel *titleLabel;
@property(nonatomic,retain)TTTAttributedLabel *contentLabel;
@property(nonatomic,retain)UIButton *mailButton;
@property(nonatomic,retain)RCMerchantDetailTrafficItem *dataModel;

@end

@implementation RCMerchantDetailTrafficCell

@synthesize titleLabel = _titleLabel;
@synthesize contentLabel = _contentLabel;
@synthesize mailButton = _mailButton;
@synthesize backgroundImageView = _backgroundImageView;
@synthesize delegate = _delegate;
@synthesize dataModel = _dataModel;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self createPlaceHolderView];
    }
    return self;
}

-(void)dealloc{
    self.delegate = nil;
    self.titleLabel = nil;
    self.contentLabel = nil;
    self.mailButton = nil;
    self.backgroundImageView = nil;
    self.dataModel = nil;
    [super dealloc];
}

-(void)createPlaceHolderView{
    
    _backgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(13, 9, 294, 100)];
    _backgroundImageView.backgroundColor = [UIColor clearColor];
    _backgroundImageView.userInteractionEnabled = YES;
    NSString *textBGFilePath = [[NSBundle mainBundle]pathForResource:kTextBGImageFile ofType:nil];
    UIImage *textBGImage = [UIImage imageWithContentsOfFile:textBGFilePath];
    textBGImage = [textBGImage resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    [_backgroundImageView setImage:textBGImage];
    [self addSubview:_backgroundImageView];
    
    _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(13, 11, 200, 16)];
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.textColor = [UIColor colorWithHex:0xff828282];
    _titleLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:16];
    [_backgroundImageView addSubview:_titleLabel];
    
    _mailButton = [[UIButton alloc]initWithFrame:CGRectMake(252, 0, 42, 38)];
    _mailButton.backgroundColor = [UIColor clearColor];
    [_mailButton setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kEmailImageFile ofType:nil]] forState:UIControlStateNormal];
    [_mailButton addTarget:self action:@selector(mailButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [_backgroundImageView addSubview:_mailButton];
    _mailButton.hidden = YES;
    
    _contentLabel = [[TTTAttributedLabel alloc]initWithFrame:CGRectMake(13, 42, 268, 100)];
    _contentLabel.backgroundColor = [UIColor clearColor];
    _contentLabel.textColor = [UIColor colorWithHex:0xff505050];
    _contentLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:14];
    _contentLabel.numberOfLines = 0;
    _contentLabel.leading = 6;
    [_backgroundImageView addSubview:_contentLabel];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

-(void)setContentWithDataModel:(RCMerchantDetailTrafficItem *)item isFirstCell:(BOOL)isFirst{
    if (nil == item) {
        return;
    }
    self.dataModel = item;
    _titleLabel.hidden = YES;
    if (![NSString stringIsNullOrEmpty:item.title]) {
        _titleLabel.hidden = NO;
        _titleLabel.text = item.title;
    }
    _contentLabel.hidden = YES;
    if (![NSString stringIsNullOrEmpty:item.content]) {
        _contentLabel.hidden = NO;
        _contentLabel.text = item.content;
        CGSize size = [_contentLabel.attributedText sizeWithConstrainedToSize:CGSizeMake(268, MAXFLOAT) Leading:6];
        CGRect rect = _contentLabel.frame;
        rect.size.height = size.height;
        _contentLabel.frame = rect;
    }
    
    if (item.type == kMerchantDetailTrafficType_Traffic) {
        _mailButton.hidden = NO;
    }else{
        _mailButton.hidden = YES;
    }
    
    CGFloat originy = 0.0f;
    if (isFirst) {
        originy = 14;
    }else{
        originy = 9;
    }
    _backgroundImageView.frame = CGRectMake(13, originy, 294, CGRectGetMaxY(_contentLabel.frame)+9);

}

+(CGFloat)getCellHeight:(RCMerchantDetailTrafficItem *)item isFirstCell:(BOOL)isFirst{
    if (nil == item) {
        return 0.0f;
    }
    TTTAttributedLabel *label = [[TTTAttributedLabel alloc]initWithFrame:CGRectMake(13, 42, 268, 100)];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor colorWithHex:0xff505050];
    label.font = [UIFont fontWithName:kFontNameLightHeiti size:14];
    label.numberOfLines = 0;
    label.leading = 6;
    label.text = item.content;
    CGSize size = [label.attributedText sizeWithConstrainedToSize:CGSizeMake(268, MAXFLOAT) Leading:6];
    CGFloat originy = 0.0f;
    if (isFirst) {
        originy = 14;
    }else{
        originy = 9;
    }
    [label release];
    return originy+42+size.height+9;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)mailButtonClicked{
    if (_delegate && [_delegate respondsToSelector:@selector(mainButtonClicked:)]) {
        [_delegate mainButtonClicked:self.dataModel.content];
    }
}

@end
